//
//  SearchView.m
//  YYFMMall
//
//  Created by chenzm on 2019/8/27.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "SearchView.h"

@interface SearchView()<UITextFieldDelegate>

@end


@implementation SearchView{
    CGFloat _icon_h;
}


#pragma mark - 赋值


#pragma mark -Methods
+ (instancetype)registSearchViewByH:(CGFloat)h{
    return [[SearchView alloc]initWithFrame:CGRectMake(0, 0, kIphone_W, h)];
}

#pragma mark - Intial
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder: aDecoder]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

///基本数据配置
-(void)setUpBaseData{
    _icon_h = 35;
}

///控件添加
-(void)setUpUI{
    [self searchTextF];
    [self rightBtn];
    [self topBtn];
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftBtn setImage:[UIImage imageNamed:@"icon_back_black"] forState:UIControlStateNormal];
    [self.leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.leftBtn.hidden = YES;
    [self addSubview:self.leftBtn];
    
    BlockSelf(_icon_h)
     [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.bottom.mas_equalTo(self).offset(-5);
         make.right.mas_equalTo(self).offset(-15);
         make.size.mas_equalTo(kSize(block_icon_h, block_icon_h));
     }];
     
     [_searchTextF mas_makeConstraints:^(MASConstraintMaker *make) {
         make.bottom.mas_equalTo(self).offset(-5);
         make.left.mas_equalTo(self).offset(15);
         make.right.mas_equalTo(self.rightBtn.mas_left).offset(-20);
         make.height.mas_equalTo(block_icon_h);
     }];
     
     [_topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.bottom.top.mas_equalTo(self);
         make.left.right.mas_equalTo(self.searchTextF);
     }];
     
     [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(kScale_W(10));
         make.centerY.mas_equalTo(self.searchTextF);
         make.size.mas_equalTo(kSize(block_icon_h, block_icon_h));
     }];
}

#pragma mark-Methods

-(void)rightBtnAction{
    [self.searchTextF resignFirstResponder];
    !self.rightBtnBlock?:self.rightBtnBlock();
}

- (void)btnClick:(UIButton *)btn {
    [[self viewController].navigationController popViewControllerAnimated:YES];
}

-(void)btnAction{
    [self.searchTextF resignFirstResponder];
    !self.txtDidEndBlock?:self.txtDidEndBlock(self.searchTextF,self.searchTextF.text);
}

-(void)topBtnAction{
    !self.topBtnBlock?:self.topBtnBlock();
}

#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    return [ZMConfigTools judgeTextF:textField range:range replaceString:string placeholder:_searchTextF.placeholder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    !self.txtDidEndBlock?:self.txtDidEndBlock(textField,textField.text);
}

//文本即将开始编译
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
//文本点击返回键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.searchTextF resignFirstResponder];
    return YES;
}

#pragma mark - 布局
-(void)layoutSubviews{
    [super layoutSubviews];
 
}

#pragma mark - lazyload

-(UITextField *)searchTextF{
    if (!_searchTextF) {
        _searchTextF = [UITextField new];
        _searchTextF.placeholder = @"大家都在搜索";
        _searchTextF.userInteractionEnabled = NO;
        _searchTextF.delegate = self;
        _searchTextF.font = kFont(14);
        _searchTextF.textColor = kBlackColor;
        //设置左侧视图的显示模式
        _searchTextF.leftViewMode = UITextFieldViewModeAlways;
        _searchTextF.leftView = [self addLeftSearchView];
        _searchTextF.rightViewMode = UITextFieldViewModeWhileEditing;
        _searchTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTextF.backgroundColor = kHexColor(0xF7F7F7);
        [self addSubview:_searchTextF];
        [ZMSpeedy zm_changeControl:_searchTextF cornerRadius:_icon_h/2 borderWidth:0 borderColor:nil mask:YES];
    }
    return _searchTextF;
}


///设置 textField 的左侧视图
-(UIView *)addLeftSearchView{
    UIImage *img = kFileImage(@"search_gray");
    CGFloat left_mar = 20;
    UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0,0, img.size.width + left_mar, img.size.height)];
    leftV.backgroundColor = [UIColor clearColor];
    
    UIImageView *imgV = [[UIImageView alloc] initWithImage:img];
    imgV.mj_size = img.size;
    imgV.mj_y = 0;
    imgV.mj_x = left_mar/2;
    
    [leftV addSubview:imgV];
    
    return leftV;
}


-(UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setImage:kImage(@"home_more_gray") forState:UIControlStateNormal];
        _rightBtn.userInteractionEnabled = NO;
        _rightBtn.titleLabel.font = kBoldFont(25);
        [_rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightBtn];
    }
    return _rightBtn;
}


-(UIButton *)topBtn{
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.userInteractionEnabled = NO;
        [_topBtn addTarget:self action:@selector(topBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_topBtn];
    }
    return _topBtn;
}


-(UIView *)lineV{
    if (!_lineV) {
        _lineV = [UIView new];
        _lineV.backgroundColor = kHexColor(0xEEEEEE);
        _lineV.hidden = YES;
        [self addSubview:_lineV];
        [_lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(1);
        }];
    }
    return _lineV;
}


-(void)dealloc{
    
}


@end
